//
//  YongQingMainVC.swift
//  YongQingFont
//
//  Created by 李东 on 2024/4/20.
//

import UIKit

class YongQingMainVC: YongQingBaseVC {
    
    @IBOutlet weak var setupBtn_top: NSLayoutConstraint!
    
    @IBOutlet weak var bottomView: UIView!
    var timer: Timer!
    var hasExtue = false
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        hideNavgaitionBar()
        setupBtn_top.constant = CGFloat.topPadding
        NotificationCenter.default.addObserver(self, selector: #selector(showAd), name: .SDKInitFinish, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(startTimer), name: .ShowFullScrrenAD, object: nil)
        ADUtils.shared.checkAdStatus()
        adHelper.didCloseFullScreenCallBack = {[weak self] in
            self?.startTimer()
        }
    }
    
    @objc func startTimer() {
        if timer == nil {
            timer = Timer(timeInterval: 30, target: self, selector: #selector(showFullScrrenAd), userInfo: nil, repeats: true)
            RunLoop.current.add(timer, forMode: .common)
        }
    }
    
    @objc func showAd() {
        if hasLoadAd {
            return
        }
        hasLoadAd = true
        adHelper.loadAD(type: .BANNER, vc: self)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if NewADTools.initFinish {
            showAd()
        }
    }
    
    @objc func showFullScrrenAd() {
        timer.invalidate()
        timer = nil
        adHelper.loadAD(type: .FULL_SCREEN)
    }
    
    
    override func viewDidLayoutSubviews() {
        view.addGradientColor(startColor: UIColor.init(hex: 0xD2ECFF), endColor: UIColor.white, startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 1, y: 1))
        bottomView.jw_corner(byRoundingCorners: [.topLeft, .topRight], radii: 20)
    }


    @IBAction func setupHandle(_ sender: Any) {
        navigationController?.pushViewController(YongQingMeVC(), animated: true)
    }
   
    @IBAction func mainItemHandle(_ sender: UIButton) {
        if !ADUtils.shared.finishTask {
            let vc = ADVideoAlertVC()
            vc.modalPresentationStyle = .overFullScreen
            present(vc, animated: true)
            return
        }
        switch sender.tag {
        case 1:
            navigationController?.pushViewController(YongQingWebVC(url: AppConfig.H5_URL), animated: true)
            break
        case 2:
            navigationController?.pushViewController(YongQingWebVC(url: AppConfig.H5_URL_Preview), animated: true)
            break
        case 3:
            navigationController?.pushViewController(YongQingUsePapersVC(), animated: true)
            break
        default:
            break
        }
    }
    
}
